
#/**********************************************************************
#@Author: Sangeeta
# This program wil be used to make revid <==> cvdid (bug introducing cvs ids)
# database
# **********************************************************************/


import MySQLdb
import re

"""
user = "root"
password="123"
database = "apsec2014"
port=3306
"""
user = "sangeetal"
password="sangeetal"
database = "apsec2014"
port=3307
#"""

tables = ["chrome_browser"]
output_table= "bugid_introducing_cvsid"

db1= MySQLdb.connect(host="localhost", user = user, passwd = password,db=database, port = port)
select_cursor = db1.cursor()
select_cursor2 = db1.cursor()
select_cursor3 =db1.cursor()
insert_cursor = db1.cursor()

#Extract revision ids having bug fixing
#Example: Issueid=358096  Broken  by https://src.chromium.org/viewvc/blink?view=rev&revision=170248 
#Example: IssueId = 338698   http://src.chromium.org/viewvc/blink?view=rev&rev=168761 
revis_re1 = "r[1-9][0-9][0-9][0-9]+"
patt1 = re.compile(revis_re1)

revis_re2 = "revision=[1-9][0-9][0-9][0-9]+"
patt2=re.compile(revis_re2)

revis_re3 = "rev=[1-9][0-9][0-9][0-9]+"
patt3 = re.compile(revis_re3)

#bug_num5 = "\[[0-9]+\]"
#plain_num = "[0-9]+"


#word = "[A-Za-z0-9]+"
#str = "hello bug#89 hkjkj bug show_bug cgi?id=899 bug \\\67"
#p = re.compile(revis_re1)
#a=p.findall(str)
#print "match = ",a

def clean(log_message):
    log_message = log_message.replace(",", "  ") 
    log_message = log_message.lower()
    return log_message
    
for tab in tables:
    select_issue_str = "select issue_id,reported  from  `issue_"+tab+ "`  where regression=1"
    select_cursor.execute(select_issue_str)
    reg_ids_db = select_cursor.fetchall()
    
    for data in reg_ids_db:
        issue_id = data[0]
        issue_timestamp = data[1]
        select_comments_str = "select comment from  `comments_"+tab+"` where  issue_id="+(str)(issue_id)
        select_cursor2.execute(select_comments_str)
        comments =select_cursor2.fetchall()
        temp_rev_ids= list()
        final_rev_ids = list()
    
        for comm in comments:
            print "comm=", comm
            rev_ids_patt1 = patt1.findall(comm[0])
            rev_ids_patt2 = patt2.findall(comm[0])
            rev_ids_patt3 = patt3.findall(comm[0])
            #rev_ids= rev_ids_patt1 + rev_ids_patt2 + rev_ids_patt3
            
            for revs1 in rev_ids_patt1:
                rev_value = revs1.split("r")[1]
                if rev_value!=32:
                    temp_rev_ids.append(rev_value)
                    
            for revs2 in rev_ids_patt2:
                rev_value = revs2.split("revision=")[1]
                if rev_value!=32:
                    temp_rev_ids.append(rev_value) 
                    
            for revs3 in rev_ids_patt3:
                rev_value = revs3.split("rev=")[1]
                if rev_value!=32:
                    temp_rev_ids.append(rev_value)                  
        
        #temp_rev_ids.append("1867");   
        print "temp_rev_ids=",temp_rev_ids 
        #buggy_rev_ids=list()
        for temp_rev in temp_rev_ids:
           select_rev = " select id, timediff(timestamp,'"+(str)(issue_timestamp)+"') as time_difference from  `revisions_"+tab+"` \
             where id="+temp_rev+ " having time_difference<0"
           print "select rev=", select_rev
           select_cursor3.execute(select_rev)
           temp_data = select_cursor3.fetchall()
           #print "temp_data=", temp_data
           if temp_data:
               final_rev_ids.append(temp_data[0][0])
       
        for rev in final_rev_ids:   
            
           insert_str = "insert into "+output_table+"_"+tab+ " values("+(str)(issue_id)+","+ (str)(rev)+")"  
           print "insert_trs=", insert_str
           try: 
            insert_cursor.execute(insert_str)
           except MySQLdb.IntegrityError, e:
             print e  
           
        db1.commit()          
            
    """
    for i 
    print "reg id=", reg_ids 
    select_str = "select id, logmessage   from comments_"+tab 
    select_cursor.execute(select_str)    
    all_commits = select_cursor.fetchall()
    
    #Re_match = list()
    #Re_score = list()
    
    for d in all_commits:
        commit_id = d[0]
        log_message = d[1]
        log_message = clean(log_message)
        
        #print "commi id=", commit_id, " log", log_message  
        ids_in_log=list()
        
        p1 = re.compile(bug_num1)
        match_pat=p1.findall(log_message)        
        #print "match pat", match_pat
        for pattern in match_pat:
            id_pat = re.compile(plain_num)
            numeric_ids = id_pat.findall(pattern)
            for id in numeric_ids:
                ids_in_log.append(int(id))
       """  
        